<template>
  <div>
    <PageTemplate>
      <div slot="content">
        <PageEditCollapse :is-search="false" :default-collapse="true">
          <div slot="title" class="page-edit-colllapse-title">
            <div style="width: 200px;">{{$t('cm.baseinfo')}}</div>
          </div>
          <div>
            <!-- 表单 -->
            <el-form
              :model="Knowledge"
              label-position="right"
              label-width="150px"
              class="page-edit-colllapse-form"
              :rules="rules"
              ref="Knowledge"
            >
              
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('sri.award_principal_user')" prop="principalUser">
                    <PersonSelect id="participant" :value="participant" @input="(v) => {participant = v}" v-bind:multiple="true"  ></PersonSelect>
                  </el-form-item>
                </el-col>
              </el-row>
              
            </el-form>
          </div>
        </PageEditCollapse>
        <el-row class="row-empty-10"></el-row>
        <el-row>
          <el-button type="primary" @click="submitForm('Knowledge')">{{$t('cm.commit')}}</el-button>
          <el-button type="primary" @click="toList">{{$t('cm.return')}}</el-button>
        </el-row>
      </div>
    </PageTemplate>
  </div>
</template>

<script>
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import PageTemplate from '@/components/common/PageTemplate'
import PageEditCollapse from '@/components/common/PageEditCollapse'
import FileUploadCommon from '@/components/common/FileUploadCommon'
import HasBtn from '@/mixins/HasBtn'
import { mapState } from 'vuex'
import { COMMON } from '@/utils/commonUtil'
import sys_manage from '@/api/sys_manage'
import PersonSelect from '@/components/asc/PersonSelect'
import PurTrackDem from './js/PurTrackDem'

const vailPrincipalUserName=(rule,value,callback)=>{
  if(document.getElementById("participant").value) {
    callback();
  }else{
    callback(new Error("必填项"));
  }
};


export default {
  components: {
    PageTemplate, PageEditCollapse, PersonSelect, FileUploadCommon
  },
  data () {
    return {
      Knowledge: {
        id: null,
        principalUser: null
      },
      participant: null,
      rules: {
        principalUser: [
          { required: true, validator: vailPrincipalUserName, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          console.log(this.participant);
          let d = {
                demandId: this.$router.history.current.params.id,
                allocationId: null,
                allocationName: null
            }
          if (this.participant != null) {           
            var temp = this.participant.split(",");
            for (var i=0; i<temp.length; i++) {
              var endIndx = temp[i].indexOf("]");
              d.allocationId = temp[i].substring(1,endIndx);
              d.allocationName = temp[i].substring(endIndx+1);
              console.log(d);
            }
          }
            
          PurTrackDem.allocation(d, result => {
            if (result.code === Constant.RT_SUCCESS) {
              ALERT.success(result.msg)
              this.toList()
            } else {
              // _this.$t('cm.tips')
              ALERT.error(result.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    toList () {
      COMMON.routerTo(Constant.ROUTE_PUR_TRACK_DEM.index, this.$router)
    }
  },
  mounted () {
  },
  computed: {
  }
}
</script>

<style lang="scss">
</style>
